const http = require("http");
const fs = require("fs");
const path = require("path");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;

function router() {

}


let server = http.createServer((req , res) => {
    console.log(req.url);
    let myurl = new URL(req.url , "http://192.168.42.166:3000/")
    console.log(myurl.pathname);
    if(req.url === '/') {
        let content = fs.readFileSync("./index.html");
        // console.log(content);
        // let dom = new JSDOM(content.toString());
        // let folder = dom.window.document.querySelector(".folder");
        res.writeHead(200 , {"content-type": "text/html"})
        // fetch(path.join("/home/wangding/node.js-listen/chapter8/文件操作/data.json")).then(res => res.json()).then(data => {console.log(data)});
        res.end(content);   
    } else if(req.url.indexOf("/image") >= 0) {
        let data = fs.readFileSync(path.join(__dirname , req.url));
        res.writeHead(200 , {"content-type":"image/png"});
        res.end(data);
    } else if(req.url.indexOf('/getlist') >= 0) {
        let data = fs.readFileSync(path.join(__dirname , "/data.json"));
        res.end(data);
    } else if(req.url.indexOf('/readFile') >= 0) {
        let filepath = '';
        console.log(123456);
        req.on("data" , (chunk) => {filepath += chunk});
        req.on("end" , () => {
            console.log(filepath);
            let content = fs.readFileSync(filepath);
            res.end(content);
        });
    }
}).listen(3000);